//
//  DKVideoController.m
//  DKShortVideo
//
//  Created by Keer_LGQ on 2018/3/31.
//  Copyright © 2018年 DK. All rights reserved.
//

#import "DKVideoController.h"
#import "DKOpenGLPlayerView.h"
#import "DKRouterManger.h"
#import "Masonry.h"

@interface DKVideoController ()

@property (nonatomic ,strong) DKOpenGLPlayerView *player;
@property (nonatomic ,strong) UIButton *btnContro;

@end

@implementation DKVideoController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.player];
    
    CGFloat width = [[UIScreen mainScreen] bounds].size.width;
    
    UIButton *btnPlay = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnPlay addTarget:self action:@selector(playVideo:) forControlEvents:UIControlEventTouchUpInside];
    [btnPlay setBackgroundColor:[UIColor redColor]];
    [btnPlay setTitle:@"播放" forState:UIControlStateNormal];
    [btnPlay setFrame:CGRectMake(0, 0, 150, 50)];
    [btnPlay setCenter:CGPointMake(width/2, 400)];
    
    _btnContro = btnPlay;
    [self.view addSubview:btnPlay];
    
    
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panDirection:)];
    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelaysTouchesBegan:YES];
    [panRecognizer setDelaysTouchesEnded:YES];
    [panRecognizer setCancelsTouchesInView:YES];
    [self.player addGestureRecognizer:panRecognizer];

    UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnBack addTarget:self action:@selector(onCloseView:) forControlEvents:UIControlEventTouchUpInside];
    [btnBack setBackgroundColor:[UIColor redColor]];
    [btnBack setTitle:@"返回" forState:UIControlStateNormal];
    [self.view addSubview:btnBack];
    [btnBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).mas_offset(25);
        make.left.equalTo(self.view).mas_offset(15);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(35);
    }];

    UIButton *btnFilter = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnFilter addTarget:self action:@selector(onFilterContro:) forControlEvents:UIControlEventTouchUpInside];
    [btnFilter setBackgroundColor:[UIColor redColor]];
    [btnFilter setTitle:@"滤镜" forState:UIControlStateNormal];
    [self.view addSubview:btnFilter];
    [btnFilter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).mas_offset(-25);
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(35);
    }];

}

- (void)dealloc
{
    NSLog(@"*******************************%@: dealloc", [self class]);
}

#pragma mark - play control
- (void)onCloseView:(UIButton *)btnBack
{
    [[DKRouterManger shareInstance] onPreviewVideo:nil isShow:NO];
}

- (void)onFilterContro:(UIButton *)btnFilter
{
    if ([btnFilter.titleLabel.text isEqualToString:@"滤镜"]) {
        [self.player setIsFilter:YES];
        [btnFilter setTitle:@"关闭滤镜" forState:UIControlStateNormal];
    }else {
        [self.player setIsFilter:NO];
        [btnFilter setTitle:@"滤镜" forState:UIControlStateNormal];
    }
}

- (void)playVideo:(UIButton *)btnPlay
{
    if ([self.player isPlaying]) {
        [self.player pause];
        [btnPlay setTitle:@"播放" forState:UIControlStateNormal];

    }else {
        [self.player play];
        [btnPlay setTitle:@"暂停" forState:UIControlStateNormal];

    }
}

#pragma mark - UIPanGestureRecognizer手势方法

/**
 *  pan手势事件
 *
 *  @param pan UIPanGestureRecognizer
 */
- (void)panDirection:(UIPanGestureRecognizer *)pan {
    // 我们要响应水平移动和垂直移动
    // 根据上次和本次移动的位置，算出一个速率的point
    CGPoint veloctyPoint = [pan velocityInView:self.view];
    
    // 判断是垂直移动还是水平移动
    switch (pan.state) {
        case UIGestureRecognizerStateBegan: { // 开始移动
            // 使用绝对值来判断移动的方向
            CGFloat x = fabs(veloctyPoint.x);
            CGFloat y = fabs(veloctyPoint.y);
            if (x < y) { // 垂直移动
                if (!self.navigationController) {
                    [[DKRouterManger shareInstance] onPreviewVideo:nil isShow:NO];
                }
            }
        }
            break;
        default:
            break;
    }
}

#pragma mark - lazy
- (DKOpenGLPlayerView *)player
{
    __weak typeof(self) weakSelf = self;
    if (!_player) {
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        _player = [[DKOpenGLPlayerView alloc] initWithFrame:CGRectMake(0, 100, width, width *9/16) autoPlay:NO];
        [_player setVideoUrl:self.videoUrl];
        [_player setPlaybackReachedEndBlock:^{
            [weakSelf.btnContro setTitle:@"播放" forState:UIControlStateNormal];
        }];
//        [_player setIsFilter:YES];
    }
    return _player;
}

@end
